#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtGui>
#include <QtNetwork/QTcpSocket>
#include <qextserialport.h>
#include "GraphicsView.h"
#include "Logger.h"
#include "LiveViewThread.h"
#include "Settings.h"
#include "PasswordDialog.h"
#include "GpioPort.h"
#include "ErrorThread.h"
#include "AboutDialog.h"
#include <windows.h>

namespace Ui {
    class MainWindow;
}

enum Status {
	TURN_ON,
	TURN_OFF,
	SETTING,
    SYSTEM_ERROR,
    PHARMACODE,
    EAN_CODE
};

enum ViewType {
	STATIC,
	LIVE
};

enum MsgType {
    MSG_INFO,
    MSG_WARN,
    MSG_ERROR
};

class MainWindow : public QMainWindow {
    Q_OBJECT

public:
	/* functions */
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

signals:
    void message(MsgType type, QString msg);

public slots:
	void loadImage();
	void getSettings();
	void send();
	void enableEdit();
	void setOcrLabel(Status status);
	void setScannerLabel(Status status);
    void setCodeLabel(Status status);
	void setViewTypeLabel(ViewType viewType);
	void getCoordinates();
	void confirmRects();
	void startOcr();
	void stopOcr();
	void startScanner();
	void stopScanner();
	void setScanner();
	void confirmScanner();
    void setCode();
    void receiveCodeString();
	void ocrStatusChanged(bool status);
	void scannerStatusChanged(bool status);
	void aboutApp();
    void showMessage(MsgType type, QString msg);

private:
    Ui::MainWindow *ui;

	/* functions */
	void initCompononets();
	void deleteComponents();
	void connectComponents();
	void closeEvent(QCloseEvent *event);
	int countRows();
	bool checkPassword();
	QByteArray createCameraString();
	void disableEdit();
	void saveSettings();
	void restoreSettings();
	bool checkControls();
	void updateSettingsWindow();
	void sendPlcString(bool tag);
    void sendCodeString();

	/* data */
	GraphicsView *view;
	QGraphicsScene scene;
	Logger logger;
	LiveViewThread liveViewThread;
	ErrorThread errorThread;
	Settings *settingsWindow;
	GpioPort *gpioPort;
	QString cameraIp;
	int cameraPort;
	QString imageFilename;
	bool gpioEnabled;
	bool lanEnabled;
	QString logFilename;
	QTcpSocket *tcpSocket;
	QextSerialPort *serialPort;
    QextSerialPort *codeSerialPort;
	bool coordinatesReady;
	QSettings settings;
	bool ocrControl;
	bool scannerControl;
	bool tagControl;
	bool capControl;
	Status lastOcrStatus;
	Status lastScannerStatus;
	int tagCapError;
	int ocrError;
    QString codePort;
    int codePart;

private slots:
	void on_actionSettings_triggered();
	void on_actionClose_triggered();
};

#endif // MAINWINDOW_H
